import * as SpiceHtml5 from '/static/spice-html5/src/main.js';


function default_handler(spice=null){}

/**
 * 封装spice相关连接操作
 * @param connect_before 连接前
 * @param connect_after 连接后
 * @param disconnect 断开
 * @returns {{disconnect: Session.disconnect, spice_onsuccess: spice_onsuccess, spice_error: spice_error, agent_connected: agent_connected, connect: connect}}
 */
export function spice(connect_before=default_handler, connect_after=default_handler,  disconnect=default_handler ) {
    let spice;
    return {
        agent_connected: function (sc) {
            window.addEventListener('resize', SpiceHtml5.handle_resize);
            window.spice_connection = this;
            SpiceHtml5.resize_helper(this);
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                var spice_xfer_area = document.createElement("div");
                spice_xfer_area.setAttribute('id', 'spice-xfer-area');
                document.getElementById('spice-area').appendChild(spice_xfer_area);
                document.getElementById('spice-area').addEventListener('dragover', SpiceHtml5.handle_file_dragover, false);
                document.getElementById('spice-area').addEventListener('drop', SpiceHtml5.handle_file_drop, false);
            }
        },
        disconnect: function () {
            if (this.spice) {
                this.spice.stop();
            }
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                var spice_xfer_area = document.getElementById('spice-xfer-area');
                if (spice_xfer_area != null) {
                    document.getElementById('spice-area').removeChild(spice_xfer_area);
                }
                document.getElementById('spice-area').removeEventListener('dragover', SpiceHtml5.handle_file_dragover, false);
                document.getElementById('spice-area').removeEventListener('drop', SpiceHtml5.handle_file_drop, false);
            }
            this.spice = null;
        },
        spice_error: function (e) {
            disconnect(this.spice)
            alert("spice连接异常!")
            this.disconnect();
        },
        spice_onsuccess: function (m) {
            connect_after(spice)
        },
        connect: function (token) {
            connect_before(this.spice);
            var host, port, password, scheme = "ws://", uri;
            host = "localhost"
            port = 0;
            password = "";
            if (this.spice) {
                this.spice.stop();
            }
            uri = scheme + window.location.href.split("/")[2] + "/websockify?token=" + token;
            try {
                this.spice = new SpiceHtml5.SpiceMainConn({
                    uri: uri,
                    screen_id: "spice-screen",
                    password: password,
                    onerror: this.spice_error,
                    onagent: this.agent_connected,
                    onsuccess: this.spice_onsuccess
                });

            } catch (e) {
                alert(e.toString());
                this.disconnect();
            }
        },
        status: function (){
            if(this.spice != null){
                return true;
            }else{
                return false;
            }
        }
    }
}